<?php
class Validate {
	
	public static function check($type, $data) {
		switch ($type) {
			case 'email':
				return self::isEmail($data);
			case 'require':
				return !self::isEmpey($data);
			case 'username':
				return self::isUserName($data);
			default:
				throw new CoreException("unknow validate type: $type");
		}
	}
	
	/**
	 * 验证是否EMAIL地址
	 *
	 * @param string $string
	 * @return boolean
	 */
	public static function isEmail($string) {
		if (preg_match('/^[0-9a-z]+[0-9a-z_\.\-]*@[0-9a-z\-]+(\.[a-z]{2,4}){1,2}$/i',$string)) {
			return true;
		}
		return false;
	}
	
	/**
	 * 验证是否为空
	 *
	 * @param string $string
	 * @return boolean
	 */
	public static function isEmpey($string) {
		$string = trim(strip_tags($string));
		return empty($string);
	}
	
	/**
	 * 是否合法的用户名
	 *
	 * @param string $string
	 * @return boolean
	 */
	public static function isUserName($string) {
		if ( empty($string) ) return FALSE;
		$badchars = array("\\",'&',' ',"'",'"','/','*',',','<','>',"\r","\t","\n",'#','$','(',')','%','@','+','?',';','^');
		foreach ($badchars as $char) {
			if (strpos($string, $char) !== false) {
				return false;
			}
		}
		return true;
	}
}
